<template>
	<view>
		<custom-navigation-bar title="商户后台登录"></custom-navigation-bar>
		<view class="main">
			 
			<view class="form-item">
				<view class="form-item-1">
					<view class="form-item-lable">
						账号
					</view>
					<view class="form-item-countent">
						<input v-model="data.username"  type="text" class="form-item-input" placeholder="请输入账号" />
					</view>
					<view class="form-item-right">
						<image src="/static/gengduo@3x(1).png" class="form-item-image"></image>
					</view>
				</view> 
			</view>  
			
			<view class="form-item">
				<view class="form-item-1">
					<view class="form-item-lable">
						密码
					</view>
					<view class="form-item-countent">
						<input v-model="data.password"  type="text" class="form-item-input" placeholder="请输入密码" />
					</view>
					<view class="form-item-right">
						<image src="/static/gengduo@3x(1).png" class="form-item-image"></image>
					</view>
				</view> 
			</view>
			  
			
			<view class="form-item">
			 	<view class="form-item-1" style="border: none;margin-top: 80rpx;margin-bottom: 80rpx;">
			 		<view class="btn" @click="submit">确定</view>
			 	</view>
			 </view>
			 
		</view>
	</view>
</template>

<script setup> 
	import CustomNavigationBar from '@/components/CustomNavigationBar.vue';
	import http from '../../../utils/http';
	const data=ref({
		username:'',
		password:'' 
	});
	
	function init(){
		wx.getStorage({
		  key: 'shopUserId',
		  success (res) {
			  if(res.data!=-1){
				 getApp().globalData.shopUserId=res.data;
				 console.log("zzzzzz",getApp().globalData.shopUserId)
				 uni.redirectTo({
				    url: "/pages/my/shop/shop"
				 })  
			  } 
		  }
		})
	}
	init();
	function submit(){
		uni.showLoading({
			title: '设置中',
			mask:true
		}); 
		http.request({
		 	url: "/x/admin/login",
		 	method: "POST",
		 	data: {
				userName:data.value.username,
				password:data.value.password 
		 	}
		}).then((responseData) => { 
			uni.hideLoading();
			if(responseData.code==1){ 
				if(responseData.data.shop==1){
					wx.setStorage({
					  key: "shopUserId",
					  data: responseData.data.user_id, 
					  success() {
						getApp().globalData.shopUserId=responseData.data.user_id
					    uni.redirectTo({
					       url: "/pages/my/shop/shop"
					    }) 
					  }
					})  
				}else{
					uni.showToast({
						icon: 'error',
						title: '非商家账号'
					})
				}
				
			}else{
				uni.showToast({
					icon: 'error',
					title: responseData.message
				})
			}
		}).catch((err) => {
			uni.hideLoading();
		 	uni.showToast({
		 		icon: 'error',
		 		title: '请求失败'
		 	})
		})
	}
</script>

<style>
	@import "../../application/form.scss"; 
</style>